package com.tyylab.kestrel.framework.core.util;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;

import java.util.List;
import java.util.stream.Collectors;

/**
 * List all classes in a package that are interfaces.
 */
public class ReflectionUtil {
    public static List<Class<?>> loadInterfaces(String packageName) {
        try (ScanResult scanResult = new ClassGraph()
                .acceptPackages(packageName)
                .scan()) {

            List<Class<?>> classes =
                scanResult.getAllClasses()
                    .filter(classInfo -> classInfo.isInterface())
                    .stream()
                    .map(ClassInfo::loadClass)
                    .collect(Collectors.toList());
            return classes;
        }
    }
}
